<template>
	<view>
		<u-popup :show="isShow" mode="center">
			<view>
				<view class='auth-header'>
					<view class="image">
						<open-data type="userAvatarUrl"></open-data>
					</view>
				</view>
				<view class='title'>授权提醒</view>
				<view class='tip'>请授权手机号，以便为您提供更好的服务</view>
				<view class='bottom acea-row'>
					<view class='item' @click='close'>随便逛逛</view>
					<button class='item grant' open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"> 授权获得手机号
					</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import store from '@/store'
	let app = getApp();
	export default {
		name: "login",
		props: {
			isShow: {
				type: Boolean,
				default: false
			},
		},
		data() {
			return {
				// isShow: false,
				code: '', //微信临时登录凭证
				Info: '',
				invite_code: '',
				openid: '',
			};
		},
		created() {
			this.invite_code = uni.getStorageSync('invite_code')
		},

		methods: {
			close() {
				// this.isShow = false
				this.$emit("close")
				uni.showToast({
					title: '您拒绝了请求,不能获得手机号',
					icon: 'error',
					duration: 2000
				});
			},

			getPhoneNumber(e) {
				console.log('getPhoneNumber', e.detail);
				this.info = e.detail
				this.getMobile()
			},

			// 用户获得手机号
			async getMobile(openid) {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/login/getMobile', //把接口传过去
					data: {
						sessionKey: uni.getStorageSync('sessionKey'),
						encryptedData: that.info.encryptedData,
						iv: that.info.iv,
						openid: uni.getStorageSync('openid')
					}
				})
				console.log('getMobile', res)
				if (res.data.status == 200) {
					that.register(res.data.data.phoneNumber)
				}

			},
			// 用户注册
			async register(mobile) {
				var that = this;
				var userInfo = uni.getStorageSync('userInfo').userInfo
				console.log(userInfo);
				const res = await this.$myRequest({
					url: '/api/login/register', //把接口传过去
					data: {
						openid: uni.getStorageSync('openid'),
						nickname:'微信用户',
						avatar:'https://dsby.hearoommed.com/avatarUrl.png',
						mobile: mobile,
						invite_code: this.invite_code
					}
				})
				console.log('register', res)
				if (res.data.status == 200) {
					that.$emit("close")
				}

			},
			//获取用户的当前设置
			getSetting() {
				return new Promise(function(resolve, reject) {
					uni.getSetting({
						success: function(res) {
							if (res.authSetting['scope.userInfo']) {
								console.log('存在');
								resolve(true);
							} else {
								console.log('不存在');
								resolve(false);
							}
						}
					})
				}).catch((e) => {
					console.log(e)
				});
			},
		}


	}
</script>

<style lang="scss" scoped>
	.auth-header {
		border-bottom: 1px solid #eee;
		text-align: center;
		padding: 50rpx 0 35rpx;
		margin: 0 20rpx;
	}

	.auth-header .image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 50%;
		display: inline-block;
		overflow: hidden;
	}

	image {
		width: 150rpx;
		height: 150rpx;
		margin: -67rpx auto 0 auto;
		display: block;
		border: 8rpx solid #fff;
		border-radius: 50%
	}

	.title {
		font-size: 28rpx;
		color: #000;
		text-align: center;
		margin-top: 30rpx
	}

	.tip {
		font-size: 22rpx;
		color: #555;
		padding: 0 24rpx;
		margin-top: 25rpx;
	}

	.bottom .item {
		width: 50%;
		height: 80rpx;
		background-color: #eeeeee;
		text-align: center;
		line-height: 80rpx;
		font-size: 24rpx;
		color: #666;
		margin-top: 54rpx;
	}

	.bottom .item.on {
		width: 100%
	}

	.bottom .item.grant {
		font-size: 28rpx;
		color: #fff;
		font-weight: bold;
		background-color: #347DFE;
		border-radius: 0;
		padding: 0;
	}

	.mask {
		position: fixed;
		top: 0;
		right: 0;
		left: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.65);
		z-index: 310;
	}
</style>
